﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace HZY.Admin.Controllers.Base//请输入命名空间
{
    using HZY.Toolkits;
    using HZY.Models;
    using HZY.Models.Sys;
    using HZY.Admin.Dto;
    using HZY.Admin.Dto.Sys;
    using HZY.Admin.Services;
    using HZY.Admin.Services.Sys;
    using HZY.EFCore.Repository;
    using Microsoft.EntityFrameworkCore;

    /// <summary>
    /// Device管理
    /// </summary>
    public class DeviceController : ApiBaseController<DeviceService>
    {
        private readonly DefaultRepository<Terminal> terminalDb;
        public DeviceController(Sys_MenuService _menuService, DeviceService _service, DefaultRepository<Terminal> terminalDb)
            : base("8f4ef300-afcd-4937-52fd-08d8312fa378", _menuService, _service)
        {
            this.terminalDb = terminalDb;
        }

        #region 页面 Views

        [HttpGet(nameof(Index))]
        public async Task<IActionResult> Index()
        {

            ViewData["Terminals"] = await this.terminalDb.Query().OrderBy(w => w.Terminal_Num).ToListAsync();

            return View();
        }

        [HttpGet("Info/{Id?}")]
        public IActionResult Info(Guid Id) => View(Id);

        #endregion

        #region 基础 CURD

        /// <summary>
        /// 查询数据列表
        /// </summary>
        /// <param name="Page"></param>
        /// <param name="Rows"></param>
        /// <param name="Search"></param>
        /// <returns></returns>
        [HttpPost("FindList/{Page}/{Rows}")]
        public async Task<ApiResult> FindListAsync(int Page, int Rows, [FromBody] Device Search)
            => this.ResultOk(await this.service.FindListAsync(Page, Rows, Search));

        /// <summary>
        /// 保存数据
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        [HttpPost("Save"), Core.HZYAppCheckModel]
        public async Task<ApiResult> SaveAsync([FromBody] Device Model)
            => this.ResultOk(await this.service.SaveAsync(Model));

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        [HttpPost("Delete")]
        public async Task<ApiResult> DeleteAsync([FromBody] List<Guid> Ids)
            => this.ResultOk(await this.service.DeleteAsync(Ids));

        /// <summary>
        /// 根据Id 加载表单数据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpPost("LoadForm/{Id?}")]
        public async Task<ApiResult> LoadFormAsync(Guid Id)
            => this.ResultOk(await this.service.LoadFormAsync(Id));

        #endregion

        /// <summary>
        /// 批量修改 设备 所属终端
        /// </summary>
        /// <param name="deviceIds"></param>
        /// <param name="terminalId"></param>
        /// <returns></returns>
        [HttpPost("ChangeTerminal/{terminalId}")]
        public async Task<ApiResult> ChangeTerminalByDeviceIds(List<Guid> deviceIds, Guid terminalId)
            => this.ResultOk(await this.service.ChangeTerminalByDeviceIds(deviceIds, terminalId));

    }
}